<template>
    <div class="home-carousel">
        <div class="showImg" @mouseover="changeInterval(true)" @mouseleave="changeInterval(false)"
            :style="{ background: mainColors }">
            <!-- 轮播图片 -->
            <img v-for="(item) in imgArr" :key="item.id" :src="item.url" alt="暂无图片" v-show="item.id === currentIndex"
                ref="img" crossorigin="anonymous" @load="getColor">
            <!-- 左侧按钮 -->
            <div @click="clickIcon('up')" class="iconDiv icon-left">
                <i class="el-icon-caret-left"></i>
            </div>
            <!-- 右侧按钮 -->
            <div @click="clickIcon('down')" class="iconDiv icon-right">
                <i class="el-icon-caret-right"></i>
            </div>
            <!-- 图片描述 -->
            <div class="img-desc">
                <span>{{ imgArr[currentIndex].text }}</span>
            </div>
            <!-- 控制圆点 -->
            <div class="banner-circle">
                <ul>
                    <li @click="changeImg(item.id)" v-for="(item) in imgArr" :key="item.id"
                        :class="item.id === currentIndex ? 'active' : ''"></li>
                </ul>
            </div>
        </div>

    </div>
</template>

<script>
import Colorthief from 'colorthief'
export default {
    name: "home-carousel",
    data() {
        return {
            currentIndex: 0,//当前所在图片下标
            timer: null,//定时轮询
            imgArr: [
                {
                    id: 0,
                    url: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
                    text: "sdadh单身单身 撒"
                },
                {
                    id: 1,
                    url: "http://39.104.22.73:8888/mongo.jpeg",
                    text: "sdadh单身单sa的撒撒"
                },
                {
                    id: 2,
                    url: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
                    text: "sdadh单身单"
                }, {
                    id: 3,
                    url: "http://39.104.22.73:8888/mongo.jpeg",
                    text: "打撒大 撒"
                },
            ],
            mainColors: "",
        }
    },
    methods: {
        //开启定时器
        startInterval() {
            // 事件里定时器应该先清除在设置，防止多次点击直接生成多个定时器
            clearInterval(this.timer);
            // 创建颜色选择器函数
            this.colorthief = new Colorthief()
            this.timer = setInterval(() => {
                this.currentIndex = (this.currentIndex + 1) % this.imgArr.length;
                this.$nextTick(() => {
                    this.getColor()
                })
            }, 3000)
        },
        // 点击左右箭头
        clickIcon(val) {
            if (val === 'down') {
                this.currentIndex = (this.currentIndex + 1) % this.imgArr.length;
            } else {
                if (this.currentIndex === 0) {
                    this.currentIndex = this.imgArr.length;
                }
                this.currentIndex--;
            }
            this.getColor()
        },
        // 点击控制圆点
        changeImg(index) {
            this.currentIndex = index
        },
        //鼠标移入移出控制
        changeInterval(val) {
            if (val) {
                clearInterval(this.timer)
            } else {
                this.startInterval()
            }
        },
        // 提取图片颜色
        async getColor() {
            const imgElem = this.$refs.img[this.currentIndex];
            imgElem.width = 100
            const colors = await this.colorthief.getPalette(imgElem, 3);
            this.mainColors = `rgb(${colors[0].join(',')})`;
        },
        // 选择主色调
        colorthief() { }
    },
    //进入页面后启动定时轮询
    mounted() {
        this.startInterval()
    },
    beforeDestroy() {
        clearInterval(this.timer);
    }
}

</script>

<style scoped>
.home-carousel {
    --cover-radio: 56.25%;
    position: relative;
    grid-column: span 2;
    grid-column: 1/3;
    grid-row: 1/3;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* 清除li前面的圆点 */
li {
    list-style-type: none;
}

.showImg {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    border-radius: 6px;
}

/* 轮播图片 */
.showImg img {
    width: 100%;
    height: 80%;
}

/* 箭头图标 */
.iconDiv {
    position: absolute;
    top: 87%;
    transform: translateY(-50%);
    width: 30px;
    height: 30px;
    border: 1px solid #666;
    border-radius: 15px;
    background-color: rgba(125, 125, 125, .2);
    line-height: 30px;
    text-align: center;
    font-size: 25px;
    cursor: pointer;
}

.iconDiv:hover {
    background-color: white;
}

.icon-left {
    right: 60px;
}

.icon-right {
    right: 10px;
}

/* 控制圆点 */
.banner-circle {
    position: absolute;
    bottom: 20px;
    left: 20px;
    width: 100%;
    height: 20px;
}

.banner-circle ul {
    height: 100%;
}

.banner-circle ul li {
    display: inline-block;
    width: 14px;
    height: 14px;
    margin: 0 5px;
    border-radius: 7px;
    background-color: rgba(125, 125, 125, .8);
    cursor: pointer;
}

.active {
    background-color: black !important;
}

.img-desc {
    position: absolute;
    left: 20px;
    bottom: 47px;
    font-size: 18px;
    font-weight: 400;
    color: white;
}
</style>